//
//  UserProfile.swift
//  GLUZSwift
//
//  Created by xyanl on 2024/9/23.
//  Copyright © 2024 考拉🐨. All rights reserved.
//
//  用户信息
import Foundation
import SwiftyUserDefaults

class UserProfile: NSObject {
    
    /// 判断当前是否登录，true：登录，false：未登录
    static var isLogin: Bool {
        !UserProfile.token.isEmpty
    }
    
    /// 获取、保存 token
    static var token: String {
        get { Defaults[\.token] }
        set { Defaults[\.token] = newValue }
    }
    
    /// 获取、保存 用户 id
    static var userId: Int {
        get { Defaults[\.userId] }
        set { Defaults[\.userId] = newValue }
    }
    
    static var identityId: Int {
        get { Defaults[\.identityId] }
        set { Defaults[\.identityId] = newValue }
    }
    
    static var mobile: String {
        get { Defaults[\.mobile] }
        set { Defaults[\.mobile] = newValue }
    }
    
    static var avatar: String {
        get { Defaults[\.avatar] }
        set { Defaults[\.avatar] = newValue }
    }
    
    /// 国家id，0：中国，1：俄罗斯
    static var country: Int {
        get { Defaults[\.country] }
        set { Defaults[\.country] = newValue }
    }
    
    static var username: String {
        get { Defaults[\.username] }
        set { Defaults[\.username] = newValue }
    }

    static var usernameRu: String {
        get { Defaults[\.usernameRu] }
        set { Defaults[\.usernameRu] = newValue }
    }
    
    /// true: 第一次启动，false：非第一次启动
    var isFirstLaunch: Bool = true
 
    static let share: UserProfile = UserProfile()
    private override init() {
        self.isFirstLaunch = true
    }
    
    static func clear() {
        token = ""
        userId = 0
        mobile = ""
        identityId = 0
        country = 0
        avatar = ""
        username = ""
        usernameRu = ""
    }
    
}

extension UserProfile {
    
    /// 身份
    static var role: IdentityType? {
        return IdentityType(rawValue: self.identityId)
    }
}
